<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>

    function red() {
        console.log('red')
    }

    function yellow() {
        console.log('yellow')
    }

    function green() {
        console.log('green')
    }

    const task = (timer, light) =>
        new Promise((resolve, reject) => {
            setTimeout(() => {
                if (light === 'red') {
                    red()
                }
                else if (light === 'green') {
                    green()
                }
                else if (light === 'yellow') {
                    yellow()
                }
                resolve()
            }, timer)
        })

    // const step = () => {
    //     task(3000, 'red')
    //         .then(() => task(2000, 'green'))
    //         .then(() => task(2100, 'yellow'))
    //         .then(step)
    // }
    //
    // step()

    // 用 async/await来实现
    const taskRunner =  async () => {
        await task(3000, 'red')
        await task(2100, 'yellow')
        await task(2000, 'green')

        taskRunner()
    }
    taskRunner()

</script>
</body>
</html>
